//导入第三模块formidable
const formidable = require('formidable');
//导入系统模块path
const path = require('path');
//导入文章集合
const article = require('../../model/article');
module.exports = async(req, res) => {
    // 创建formidable解析对象
    let form = new formidable.IncomingForm();
    //设置文件存储目录
    form.uploadDir = path.join(path.join(__dirname, '../', '../', 'public', 'uploads'));
    form.keepExtensions = true;
    form.parse(req, async(err, fields, files) => {
        //err错误对象如果表单解析失败 err里面存储错误信息，如果表单解析成功 err 为null
        //fields 对象类型保存表单数据
        //files 对象类型保存上传文件相关的数据
        // res.send(files);
        // res.send(files.cover.path);
        await article.create({
            title: fields.title,
            author: fields.author,
            publishDate: fields.publishDate,
            cover: files.cover.path.split('public')[1],
            content: fields.content,
        });
        res.redirect('/admin/article');
    });

}